package com.offcn.order.controller;

import com.offcn.entity.Result;
import com.offcn.entity.StatusCode;
import com.offcn.order.pojo.Order;
import com.offcn.order.service.OrderService;
import com.offcn.utils.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/add")
    public Result add(@RequestBody Order order) {
        // 获取令牌并解析，得到用户名
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String token = details.getTokenValue();
        // 获取用户名
        String username = TokenDecode.getUsername(token);
        order.setUserId(username);
        orderService.add(order);
        return new Result(true, StatusCode.OK,"生成订单成功");
    }

    @RequestMapping("/updateOrderStatus")
    public Result updateOrderStatus(
            @RequestParam("out_trade_no") String out_trade_no,
            @RequestParam("trade_no") String trade_no) {
        orderService.updateStatus(out_trade_no,trade_no);
        return new Result(true,StatusCode.OK,"修改订单状态成功");
    }
}
